package yxy.game.pm2.active.module;

import org.apache.commons.lang3.RandomStringUtils;
import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.module.BaseGameRedisBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

/**
 * 兑换码
 * @author Dell
 *
 */
public class ActiveModule_兑换码 extends BaseGameRedisBean {

	//bean ----------------------------------
	public static final int acttype = 108;
	
	private String redeemcode;		//兑换码
	private int redeemtype;			//兑换类型
	private String batch;			//批次
	private ArrayList<Currency> curs;	//奖励信息
	private ArrayList<String> pcodes;	//领取玩家code码
	private DateTime losetime;		//失效时间
	private String name;			//兑换码名称
	private int state;				//状态
	
	public String getRedeemcode() {
		return redeemcode;
	}
	public void setRedeemcode(String redeemcode) {
		this.redeemcode = redeemcode;
	}
	public int getRedeemtype() {
		return redeemtype;
	}
	public void setRedeemtype(int redeemtype) {
		this.redeemtype = redeemtype;
	}
	public String getBatch() {
		return batch;
	}
	public void setBatch(String batch) {
		this.batch = batch;
	}
	public ArrayList<Currency> getCurs() {
		return curs;
	}
	public void setCurs(ArrayList<Currency> curs) {
		this.curs = curs;
	}
	public ArrayList<String> getPcodes() {
		return pcodes;
	}
	public void setPcodes(ArrayList<String> pcodes) {
		this.pcodes = pcodes;
	}
	public DateTime getLosetime() {
		return losetime;
	}
	public void setLosetime(DateTime losetime) {
		this.losetime = losetime;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}

	//func ----------------------------------
//	0：opoc:一人一码，一个玩家只能使用一个兑换码，兑换后不能再使用其他兑换码并且该兑换码失效
//	1：opnc:一人多码，一个玩家可以使用多个兑换码，同一个码使用后即失效
//	2：npoc:多人一码，一个兑换码可以被多个玩家使用，玩家领取奖励后，不能再次领取

	/** 一人一码  **/
	public static final int opoc = 0;
	/** 一人多码  **/
	public static final int opnc = 1;
	/** 多人一码   **/
	public static final int npoc = 2;
	
	public static final int effective = 1;	//有效的
	public static final int invalid = 2;	//无效
	public static final int getover = 3;	//已领取
	
	static public ActiveModule_兑换码 create(String redeemcode, int redeemtype, String batch, DateTime losetime, String name, ArrayList<Currency> curs) {
		ActiveModule_兑换码 am = new ActiveModule_兑换码();
		am.setRedeemcode(redeemcode);
		am.setRedeemtype(redeemtype);
		am.setBatch(batch);
		am.setLosetime(losetime.withHourOfDay(23).withMinuteOfHour(30).withSecondOfMinute(0).withMillisOfSecond(0));
		am.setName(name);
		am.setPcodes(new ArrayList<String>());
		am.setState(effective);
		am.setCurs(curs);
		am.set();
		return am;
	}
	
	static public ActiveModule_兑换码 create(int redeemtype, String batch, DateTime losetime, String name, ArrayList<Currency> curs) {
		return create(createredeemcode(), redeemtype, batch, losetime, name,  curs); 
	}
	
	
	static public ActiveModule_兑换码 create(String redeemcode, int redeemtype, DateTime losetime, String name, ArrayList<Currency> curs) {
		ActiveModule_兑换码 am = new ActiveModule_兑换码();
		am.setRedeemcode(redeemcode);
		am.setRedeemtype(redeemtype);
		am.setBatch(createbatch());
		am.setLosetime(losetime.withHourOfDay(23).withMinuteOfHour(30).withSecondOfMinute(0).withMillisOfSecond(0));
		am.setName(name);
		am.setPcodes(new ArrayList<String>());
		am.setState(effective);
		am.setCurs(curs);
		am.set();
		return am;
	}
	
	static public ActiveModule_兑换码 create(int redeemtype, DateTime losetime, String name, ArrayList<Currency> curs) {
		return create(createredeemcode(), redeemtype, losetime, name, curs);
	}
	
	/**
	 * 产生8位随机字符兑换码
	 * @return
	 */
	static public String createredeemcode() {
		String redeemcode = RandomStringUtils.randomAlphabetic(8);
		ActiveModule_兑换码 am = ActiveModule_兑换码.get(redeemcode);
		while (am != null) {
			redeemcode = RandomStringUtils.randomAlphabetic(8);
			am = ActiveModule_兑换码.get(redeemcode);
		}
		return redeemcode;
	}
	
	/**
	 * yyyyMMddHHmmss 格式时间批次
	 * @return
	 */
	static public String createbatch() {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	}
	
	//redis ---------------------------------
	static private String key(String redeemcode) {
		return String.format("REDEEMCODE:%s", redeemcode);
	}

	public void set() {
		try {
			setv(key(getRedeemcode()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public ActiveModule_兑换码 get(String redeemcode) {
		try {
			ActiveModule_兑换码 module = getv(key(redeemcode));
			return module;
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/** 正常情况下不要使用这个方法 */
	static public Collection<ActiveModule_兑换码> all() {
		ArrayList<ActiveModule_兑换码> list = new ArrayList<>();
		try {
			Iterable<String> iterable = redis().client().getKeys().getKeysByPattern(key("*"));
			for (String key : iterable) {
				ActiveModule_兑换码 am = getv(key);
				if (am != null) {
					list.add(am);
				}
			}
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getRedeemcode()));
	}
}
